1 00:00:00,630 --> 00:00:01,260 Welcome. 2 00:00:01,260 --> 00:00:06,680 In this lecture, we're going to explore the difference between server scripts and local scripts. 3 00:00:06,690 --> 00:00:13,140 As many beginners get lost when to use these two different scripts, what's important to understand 4 00:00:13,170 --> 00:00:15,510 is that Roblox is an online game. 5 00:00:15,510 --> 00:00:20,940 Meaning whenever you join an experience, you download a copy of that game onto your computer, meaning 6 00:00:20,940 --> 00:00:24,330 you download all the audio, the parts, the scripts, and so on. 7 00:00:24,360 --> 00:00:28,980 Your computer is rendering a copy of the game that is separate from the server. 8 00:00:28,980 --> 00:00:34,860 Whenever the server makes changes to the game, the server tells you to do that as well on your copy, 9 00:00:35,040 --> 00:00:40,410 the server or server scripts, which I'll create real quick here in the workspace. 10 00:00:41,050 --> 00:00:45,250 These scripts make changes to the game that all players can see. 11 00:00:45,280 --> 00:00:51,520 This can be things such as updating leaderboards, spawning in NPCs, changing maps, giving players 12 00:00:51,520 --> 00:00:52,930 tools, and so on. 13 00:00:52,960 --> 00:00:58,330 The purpose of server scripts is for you to code logic in your game that needs to affect all players. 14 00:00:58,360 --> 00:01:03,100 There also used to verify information coming from clients through remote events, which we're going 15 00:01:03,100 --> 00:01:05,230 to talk about more in a future course. 16 00:01:06,150 --> 00:01:11,100 Most of the time your server scripts are going to be located in the server script service container, 17 00:01:11,100 --> 00:01:14,880 and this is a special service used for organizing our server scripts. 18 00:01:14,910 --> 00:01:18,600 No clients or players in our game have access to this service. 19 00:01:18,630 --> 00:01:20,730 It's reserved only for the server. 20 00:01:20,820 --> 00:01:25,980 Sometimes you may want to put server scripts inside of the workspace as well, which is fine, but most 21 00:01:25,980 --> 00:01:29,130 of the time you'll want to put them inside of the server script service. 22 00:01:29,160 --> 00:01:31,800 We also have the server storage service. 23 00:01:31,800 --> 00:01:36,970 If we ever need to store any objects in our game that do not need to be replicated to all players. 24 00:01:36,990 --> 00:01:40,620 The service is also private and can only be accessed by the server. 25 00:01:41,360 --> 00:01:43,580 Now a local script, which I'll create real quick. 26 00:01:43,580 --> 00:01:50,480 Here in the workspace, a local script is used for when we want to affect only our copy of the game. 27 00:01:50,480 --> 00:01:55,190 Local scripts have access to client side features, such as listening to inputs from your mouse and 28 00:01:55,190 --> 00:01:55,800 keyboard. 29 00:01:55,820 --> 00:02:01,010 The server is unable to do this because the server is separate from your computer being hosted on a 30 00:02:01,010 --> 00:02:02,720 computer or somewhere else in the world. 31 00:02:02,720 --> 00:02:06,980 Therefore, server scripts are unable to listen to inputs from your computer. 32 00:02:07,010 --> 00:02:11,830 If we wanted to script the functionality for our GUI on our screen, we would have to use a local script 33 00:02:11,840 --> 00:02:17,570 if we wanted to cause a change in our game's workspace that is only visible on our copy of the game. 34 00:02:17,570 --> 00:02:21,890 Again, we would have to use a local script if we wanted to manipulate our camera. 35 00:02:21,890 --> 00:02:23,700 We must use a local script. 36 00:02:23,720 --> 00:02:30,110 Any changes made with a local script is only visible to our copy of the game, and it does not replicate 37 00:02:30,110 --> 00:02:32,480 to the server or any other players. 38 00:02:32,690 --> 00:02:36,950 For a local script to execute, we must place them in a valid container. 39 00:02:36,950 --> 00:02:37,910 Services. 40 00:02:37,940 --> 00:02:44,090 These services are either the starter pack, which we use for tools, starter GUI which is used for 41 00:02:44,090 --> 00:02:46,910 screen GUI, and then inside of starter player. 42 00:02:46,910 --> 00:02:52,400 We have starter player scripts, which we use for general client side functionality, such as listening 43 00:02:52,400 --> 00:02:56,360 for when our player dies and displaying a death message in the screen and stuff like that. 44 00:02:56,360 --> 00:03:00,890 And then we have a starter character scripts, which is used for affecting our player's character, 45 00:03:00,920 --> 00:03:02,510 like health regeneration. 46 00:03:02,510 --> 00:03:07,700 When our player's character dies, any character scripts get deleted and a new set of those scripts 47 00:03:07,730 --> 00:03:10,190 get placed in the player's new character. 48 00:03:10,220 --> 00:03:14,930 The last container we can place local scripts inside of is going to be replicated first. 49 00:03:14,960 --> 00:03:20,300 Typically, you only use local scripts in here for loading screens when your player first joins the 50 00:03:20,300 --> 00:03:20,720 game. 51 00:03:20,720 --> 00:03:24,320 Otherwise, we don't use replicated first for storing local scripts. 52 00:03:25,070 --> 00:03:30,950 So if a local script is not stored and these five containers, then they're not going to execute. 53 00:03:30,980 --> 00:03:36,560 The only exception would be scripts that are stored in a player's character model, but otherwise we 54 00:03:36,560 --> 00:03:40,700 cannot execute any local scripts that are in, for example, replicated storage. 55 00:03:40,700 --> 00:03:44,870 We can't execute them if they're in the lighting service or if they're in the workspace. 56 00:03:44,900 --> 00:03:48,230 It's only within these particular services. 57 00:03:48,840 --> 00:03:55,080 So to recap, we use server scripts to cause changes that all players can see, and we use local scripts 58 00:03:55,080 --> 00:03:59,250 to cause changes that only one player can see on their copy of the game. 59 00:03:59,280 --> 00:04:03,540 So let's go ahead and take a look at the difference between local scripts and server scripts. 60 00:04:03,750 --> 00:04:08,070 What I'm going to do is I'm going to take this local script that I've created in the workspace, and 61 00:04:08,070 --> 00:04:14,610 I'm going to change the parent property to be inside of starter player scripts inside of my local script. 62 00:04:14,640 --> 00:04:16,590 All I'm going to do is create a new variable. 63 00:04:16,590 --> 00:04:20,610 I'm going to call it part, and I want to create a new part in my game. 64 00:04:20,610 --> 00:04:24,030 And we can do so using the instance dot new function. 65 00:04:24,030 --> 00:04:27,810 And then we pass the class name of the new object we would like to create. 66 00:04:27,810 --> 00:04:30,600 In this case we would like to create a new part. 67 00:04:30,900 --> 00:04:33,450 From this point we can refer to this part. 68 00:04:33,450 --> 00:04:36,660 And I want to change the parent of this part equal to the workspace. 69 00:04:36,660 --> 00:04:40,380 That way it renders on our screen inside of the game. 70 00:04:40,470 --> 00:04:43,020 And then I also want to make sure that it is anchored. 71 00:04:43,020 --> 00:04:44,790 So physics are not being applied. 72 00:04:44,790 --> 00:04:46,230 So we'll set anchor to true. 73 00:04:46,530 --> 00:04:49,650 And then we're going to change the position of our part. 74 00:04:51,070 --> 00:04:52,180 Equal to. 75 00:04:52,180 --> 00:04:55,720 And we're going to use something called a vector three dot new. 76 00:04:55,750 --> 00:04:57,640 We're going to learn more about vector three later. 77 00:04:57,640 --> 00:05:03,790 But just for now we've got to pass an x, y and z position so we could set it at like ten, ten and 78 00:05:03,790 --> 00:05:04,420 ten. 79 00:05:04,750 --> 00:05:09,310 So now this local script, whenever a player joins our game, we're going to get a copy of this local 80 00:05:09,310 --> 00:05:10,750 script on our computer. 81 00:05:10,750 --> 00:05:14,620 And we're going to be executing all of the code inside of this local script. 82 00:05:14,620 --> 00:05:17,860 And this is going to happen on our copy of the game. 83 00:05:17,860 --> 00:05:19,690 So we're going to create a new part. 84 00:05:19,720 --> 00:05:22,180 We're setting the parent of that part equal to the workspace. 85 00:05:22,180 --> 00:05:27,370 And we're setting it at the position of ten, ten, ten inside of the workspace. 86 00:05:27,640 --> 00:05:31,840 So if we go ahead and play test our game by hitting play. 87 00:05:33,230 --> 00:05:34,700 And we try to look for our part. 88 00:05:34,730 --> 00:05:35,470 There it is. 89 00:05:35,480 --> 00:05:40,940 We have a new part inside of the workspace, and it has spawned at the position of ten, ten, ten. 90 00:05:40,970 --> 00:05:47,330 However, if we go ahead and change to the server when we look on the server here, as you can see, 91 00:05:47,330 --> 00:05:49,490 we cannot see our part anywhere. 92 00:05:49,490 --> 00:05:54,320 And that's because again, that local script is only affecting our copy of the game. 93 00:05:54,320 --> 00:05:58,430 The server can't see it, it's only visible on our game right here. 94 00:05:58,430 --> 00:06:01,940 And that means we can also interact with this part, such as jumping on it. 95 00:06:02,150 --> 00:06:03,650 So here we are. 96 00:06:03,650 --> 00:06:08,720 I'm standing on my part, but if I go and look on the server, as you can see, the server can't see 97 00:06:08,720 --> 00:06:09,050 the part. 98 00:06:09,080 --> 00:06:09,830 There's nothing here. 99 00:06:09,830 --> 00:06:12,560 And it just looks like our player's character is floating. 100 00:06:13,190 --> 00:06:13,550 Now. 101 00:06:13,550 --> 00:06:17,030 Instead what I'm going to do as I'm going to copy this code. 102 00:06:18,080 --> 00:06:23,390 And I'm going to delete it out of here and then place it inside of my server script. 103 00:06:23,600 --> 00:06:26,210 And this is going to do the exact same thing. 104 00:06:26,210 --> 00:06:32,000 But this time since the server is creating it, it should be visible for my copy of the game and it 105 00:06:32,000 --> 00:06:34,350 should also be visible on the server as well. 106 00:06:34,370 --> 00:06:36,350 So if we go ahead and hit play. 107 00:06:37,340 --> 00:06:38,720 And we go and look around. 108 00:06:38,750 --> 00:06:39,470 There we are. 109 00:06:39,500 --> 00:06:40,640 There is our part. 110 00:06:40,640 --> 00:06:45,650 And if we switch to the view on the server, as you can see, the server can see it as well. 111 00:06:46,340 --> 00:06:50,600 Now, let's say I had a local script here on the client that went and deleted this part. 112 00:06:50,600 --> 00:06:54,530 So I'm just going to select it here and I'm just going to delete the part. 113 00:06:54,530 --> 00:06:56,320 So I'm going to hit delete on my keyboard. 114 00:06:56,330 --> 00:06:59,090 Now it is gone out of our copy of the game. 115 00:06:59,090 --> 00:07:02,900 And then if I go back on the server as you can see it's still here. 116 00:07:02,930 --> 00:07:03,380 Right? 117 00:07:03,380 --> 00:07:08,270 But on our local copy of the game, we don't see the part anymore because we deleted it. 118 00:07:08,980 --> 00:07:13,030 And again, this applies for all properties and objects in our game. 119 00:07:13,030 --> 00:07:19,480 Whenever a server script makes a change to an object or a property, all players in the game will observe 120 00:07:19,480 --> 00:07:20,400 that change. 121 00:07:20,410 --> 00:07:26,830 But if a player on their local copy of the game makes a change to an object or a property through a 122 00:07:26,830 --> 00:07:30,640 local script, it's only going to be visible on that player's copy of the game. 123 00:07:30,640 --> 00:07:34,700 No other player is going to be able to see it, and the server is not going to be able to see it either. 124 00:07:34,720 --> 00:07:37,090 And this works, as I said, for all properties. 125 00:07:37,090 --> 00:07:43,450 So for example, if I were to refer to the workspace and get the spawn location and let's say I wanted 126 00:07:43,450 --> 00:07:48,040 to change the color to something new, and we do so using a color three dot new. 127 00:07:48,040 --> 00:07:50,560 Again, we're going to be learning about these data types later. 128 00:07:50,560 --> 00:07:54,700 But for now I'm just going to change it to let's say we want to change it to a blue color. 129 00:07:57,030 --> 00:07:59,760 So then we go and playtest our game again. 130 00:08:00,560 --> 00:08:07,480 As you can see, our spawn location is now blue, but if we go look at the server, it's also blue, 131 00:08:07,490 --> 00:08:08,930 and then we go back to the client. 132 00:08:08,930 --> 00:08:12,740 If the client through a script decides to change the color. 133 00:08:12,890 --> 00:08:15,020 Let's say we change it to green. 134 00:08:16,220 --> 00:08:21,470 We can see that it's green on our copy of the game, but if we go and look on the server, it is still 135 00:08:21,500 --> 00:08:22,130 blue. 136 00:08:23,120 --> 00:08:23,720 All righty. 137 00:08:23,720 --> 00:08:27,840 I hope that cleared up the differences between local scripts and server scripts. 138 00:08:27,860 --> 00:08:31,640 That's all for me for this lecture, and I will see you in the next one.